
package com.heimali.happyreader.core.net;

import com.heimali.happyreader.core.bean.User;
import com.heimali.happyreader.utils.HttpUtils;
import com.heimali.happyreader.utils.UserUtils;

import java.net.URI;

public class UserApi {
    public static String Tag = "UserApi";

    private final String PROTOCOL = "http";
    private final String HOST = "os.heimali.com";
    private final int PORT = 80;

    private final String SERVER_PATH = "/HappyReader/index.php";

    public User login(String username, String password) {
        String query = HttpUtils.buildQuery(
                new String[] {
                        "type", "checkuser"
                },
                new String[] {
                        "username", UserUtils.getUserName(username, password)
                },
                new String[] {
                        "password", UserUtils.getUserName(password, username)
                });
        String content = Request.get(buildURI(SERVER_PATH, query));
        return JsonParser.loginInfoParser(content);
    }

    public User register(String username, String password) {
        String query = HttpUtils.buildQuery(
                new String[] {
                        "type", "adduser"
                },
                new String[] {
                        "username", UserUtils.getUserName(username, password)
                },
                new String[] {
                        "password", UserUtils.getUserName(password, username)
                });
        String content = Request.get(buildURI(SERVER_PATH, query));
        return JsonParser.loginInfoParser(content);
    }

    private URI buildURI(String uri, String query) {
        return HttpUtils.buildURI(PROTOCOL, HOST, PORT, uri, query);
    }
}
